package com.tkm.expressions.lambdas

fun main() {
    val fun1: () -> Unit = fun() {
        println("Hello world!")
    }

    //  Kotlin的lambda表达式为匿名函数的语法糖
    val fun2: () -> Unit = {
        println("Hello world!")
    }

    val fun3: Function0<Unit> = {
        println("Hello world!")
    }

    fun1()
    fun2()
    fun3()

    val f1: (Int) -> String = fun(p: Int): String {
        println(p)
        return "Hello world!"
    }

    //  lambda表达式的返回值就是lambda表达式最后一行的值
    val f2: (Int) -> String = { p: Int ->
        println(p)
        "Hello world!"
    }

    val f3: Function1<Int, String> = { p: Int ->
        println(p)
        "Hello world!"
    }

    println(f1(10))
    println(f2(10))
    println(f3(10))

    IntArray(10, { index -> index + 1 })
    IntArray(10, { it + 1 })
    IntArray(10) { index -> index + 1 }
    IntArray(10) { it + 1 }

    val initFn: Function1<Int, Int> = { it + 1 }
    IntArray(10, initFn)
}
